From 42b52a767947e7d6e7bf1293148113f1dddfa636 Mon Sep 17 00:00:00 2001 From: robertlipe Date: Wed, 1 Jan 2014 23:25:38 +0000 Subject: [PATCH] Try to untangle the uncodings in gdb. I'm not really sure if I made it better or worse...This is not one of our formats that makes me proud. --- gpsbabel/easygps.cc | 10 ++---- gpsbabel/gbfile.cc | 11 +++++- gpsbabel/gbfile.h | 5 +-- gpsbabel/gdb.cc | 82 +++++++++++++++++++++++-------------------- gpsbabel/mapsource.cc | 15 ++++---- 5 files changed, 65 insertions(+), 58 deletions(-) diff --git a/gpsbabel/easygps.cc b/gpsbabel/easygps.cc index c446aa9ae..7c25eb0ae 100644 --- a/gpsbabel/easygps.cc +++ b/gpsbabel/easygps.cc @@ -112,19 +112,13 @@ data_read(void) wpt_tmp->notes = gbfgetcstr(file_in); break; case 9: { /* NULL Terminated (vs. pascal) link */ - char* url = gbfgetcstr(file_in); + QString url = gbfgetcstr(file_in); link.url_ = url; - if (url) { - xfree(url); - } } break; case 0x10: { - char* ult = gbfgetcstr(file_in); + QString ult = gbfgetcstr(file_in); link.url_link_text_ = ult; - if (ult) { - xfree(ult); - } } break; case 0x63: diff --git a/gpsbabel/gbfile.cc b/gpsbabel/gbfile.cc index 60ffaaa14..1b5d3f542 100644 --- a/gpsbabel/gbfile.cc +++ b/gpsbabel/gbfile.cc @@ -981,7 +981,7 @@ gbfgetflt(gbfile* file) */ char* -gbfgetcstr(gbfile* file) +gbfgetcstr_old(gbfile* file) { char* result; int len = 0; @@ -1011,6 +1011,15 @@ gbfgetcstr(gbfile* file) return result; } +QString +gbfgetcstr(gbfile* file) +{ + char* result = gbfgetcstr_old(file); + QString rv(result); + xfree(result); + return rv; +} + /* * gbfgetpstr: Reads a pascal string (first byte is length) from file. * The result is a temporary allocated entity: use it or free it! diff --git a/gpsbabel/gbfile.h b/gpsbabel/gbfile.h index 72c1bde5c..669dd2073 100644 --- a/gpsbabel/gbfile.h +++ b/gpsbabel/gbfile.h @@ -123,8 +123,9 @@ int16_t gbfgetint16(gbfile* file); double gbfgetdbl(gbfile* file); // read a double value float gbfgetflt(gbfile* file); // read a float value char* gbfgetstr(gbfile* file); // read until any type of line-breaks or EOF -QString gbfgetpstr(gbfile* file); // read a pascal string -char* gbfgetcstr(gbfile* file); // read a null terminated string +QString gbfgetpstr(gbfile* file); // read a pascal string +QString gbfgetcstr(gbfile* file); // read a null terminated string +char* gbfgetcstr_old(gbfile* file); // read a null terminated string int gbfputint16(const int16_t i, gbfile* file); #define gbfputuint16(a,b) gbfputint16((uint16_t)(a),(b)) diff --git a/gpsbabel/gdb.cc b/gpsbabel/gdb.cc index e36bec896..af9cf1a24 100644 --- a/gpsbabel/gdb.cc +++ b/gpsbabel/gdb.cc @@ -209,18 +209,41 @@ disp_summary(const gbfile* f) #define FREAD(a,b) gbfread(a,(b),1,fin) #define FREAD_i32 gbfgetint32(fin) #define FREAD_i16 gbfgetint16(fin) -#define FREAD_STR(a) gdb_fread_str(a,sizeof(a),fin) -#if NEW_STRINGS -#define FREAD_CSTR \ - (gdb_ver >= GDB_VER_UTF8) ? QString::fromUtf8(gdb_fread_cstr(fin)) : \ - QString::fromLatin1(gdb_fread_cstr(fin)) -#else -#define FREAD_CSTR gdb_fread_cstr(fin) -#endif -#define FREAD_CSTR_AS_QSTR gdb_fread_cstr_as_qstr(fin) #define FREAD_DBL gbfgetdbl(fin) #define FREAD_LATLON GPS_Math_Semi_To_Deg(gbfgetint32(fin)) +#define FREAD_STR(a) gdb_fread_str(a,sizeof(a),fin) + +// This is all very messy. Some versions of GDB store strings as +// 8859-1 strings and others as UTF8. This wrapper tries to hide +// all that while (while keeping the character sets correct) and +// not pushing that decision down into gbfread. This module is +// still pretty messy and the points as to which fields are encode +// which ways in which versions are not at all clear, leaing to +// encoding issues on read and leaks because of teh differences +// in calling conventions on who owns/destroys the result. +//#define FREAD_CSTR \ +// (gdb_ver >= GDB_VER_UTF8) ? QString::fromUtf8(gdb_fread_cstr(fin)) : \ +// QString::fromLatin1(gdb_fread_cstr(fin)) +#define FREAD_CSTR_AS_QSTR gbfgetcstr(fin) + +static char* gdb_fread_cstr(gbfile* fin); + +QString fread_cstr() +{ + QString rv; + char* s = gdb_fread_cstr(fin); + if (gdb_ver >= GDB_VER_UTF8) { + rv = QString::fromUtf8(s); + } else { + rv = QString::fromLatin1(s); + } + + xfree(s); + + return rv; +} + #if GDB_DEBUG static char* nice(const char* str) @@ -257,22 +280,14 @@ nice(const char* str) static char* gdb_fread_cstr(gbfile* fin) { - char* result = gbfgetcstr(fin); + char* result = gbfgetcstr_old(fin); if (result && (*result == '\0')) { xfree(result); result = NULL; } - return result; -} -static QString -gdb_fread_cstr_as_qstr(gbfile* fin) -{ - char* result = gdb_fread_cstr(fin); - QString qresult = result; - xfree(result); - return qresult; + return result; } static int @@ -303,21 +318,10 @@ gdb_fread_strlist(void) count = FREAD_i32; while (count > 0) { -#if HUH - char* str = FREAD_CSTR; - if (str != NULL) { - if (*str && (res == NULL)) { - res = str; - } else { - xfree(str); - } - } -#else - QString str = FREAD_CSTR; + QString str = fread_cstr(); if (!str.isEmpty()) { res = str; } -#endif count--; } @@ -555,7 +559,7 @@ read_waypoint(gt_waypt_classes_e* waypt_class_out) gmsd = garmin_fs_alloc(-1); fs_chain_add(&res->fs, (format_specific_data*) gmsd); - res->shortname = FREAD_CSTR; + res->shortname = fread_cstr(); #if GDB_DEBUG sn = xstrdup(nice(res->shortname)); #endif @@ -598,7 +602,7 @@ read_waypoint(gt_waypt_classes_e* waypt_class_out) res->latitude < 0 ? 'S' : 'N', res->latitude, res->longitude < 0 ? 'W' : 'E', res->longitude); #endif - res->notes = FREAD_CSTR; + res->notes = fread_cstr(); #if GDB_DEBUG DBG(GDB_DBG_WPTe, res->notes) { char* str = gstrsub(res->notes, "\r\n", ", "); @@ -691,7 +695,7 @@ read_waypoint(gt_waypt_classes_e* waypt_class_out) GMSD_SETSTR(addr, bufp); FREAD(buf, 5); /* instruction depended */ - res->description = FREAD_CSTR; /* instruction */ + res->description = FREAD_CSTR_AS_QSTR; /* instruction */ url_ct = FREAD_i32; for (i = url_ct; (i); i--) { QString str = FREAD_CSTR_AS_QSTR; @@ -801,7 +805,7 @@ read_route(void) warnings = 0; rte = route_head_alloc(); - rte->rte_name = FREAD_CSTR; + rte->rte_name = fread_cstr(); FREAD(buf, 1); /* display/autoname - 1 byte */ if (FREAD_C == 0) { /* max. data flag */ @@ -838,7 +842,7 @@ read_route(void) wpt = waypt_new(); rtept_ct++; - wpt->shortname = FREAD_CSTR; /* shortname */ + wpt->shortname = fread_cstr(); /* shortname */ wpt_class = FREAD_i32; /* waypoint class */ FREAD_STR(buf); /* country code */ FREAD(buf, 18 + 4); /* subclass part 1-3 / unknown */ @@ -979,7 +983,7 @@ read_route(void) /* VERSION DEPENDENT CODE */ if (gdb_ver <= GDB_VER_2) { - rte->rte_url = FREAD_CSTR_AS_QSTR; + rte->rte_url = fread_cstr(); } else { rte->rte_url = gdb_fread_strlist(); @@ -987,7 +991,7 @@ read_route(void) rte->line_color.bbggrr = gt_color_value(color_idx); FREAD(buf, 1); /* ?????????????????????????????????? */ - rte->rte_desc = FREAD_CSTR; + rte->rte_desc = fread_cstr(); #if 0 /* replace CRLF's with ", " */ if (rte->rte_desc) { @@ -1015,7 +1019,7 @@ read_track(void) trk_ct++; res = route_head_alloc(); - res->rte_name = FREAD_CSTR; + res->rte_name = fread_cstr(); // res->rte_num = trk_ct; FREAD(&dummy, 1); /* display - 1 byte */ diff --git a/gpsbabel/mapsource.cc b/gpsbabel/mapsource.cc index ab8acb849..4ceb34b09 100644 --- a/gpsbabel/mapsource.cc +++ b/gpsbabel/mapsource.cc @@ -551,7 +551,7 @@ mps_waypoint_r(gbfile* mps_file, int mps_ver, waypoint** wpt, unsigned int* mpsc gbfseek(mps_file, 8, SEEK_CUR); } - QScopedPointerwptdesc (gbfgetcstr(mps_file)); + QString wptdesc = gbfgetcstr(mps_file); if (gbfgetc(mps_file) == 1) { /* proximity validity */ mps_proximity = gbfgetdbl(mps_file); @@ -579,14 +579,13 @@ mps_waypoint_r(gbfile* mps_file, int mps_ver, waypoint** wpt, unsigned int* mpsc if ((mps_ver == 4) || (mps_ver == 5)) { gbfread(tbuf, 6, 1, mps_file); /* unknown */ - QScopedPointerwptnotes (gbfgetcstr(mps_file)); - thisWaypoint->notes = wptnotes.take(); + thisWaypoint->notes = gbfgetcstr(mps_file); } else { gbfread(tbuf, 2, 1, mps_file); /* unknown */ } thisWaypoint->shortname = wptname; - thisWaypoint->description = wptdesc.take(); + thisWaypoint->description = wptdesc; thisWaypoint->latitude = GPS_Math_Semi_To_Deg(lat); thisWaypoint->longitude = GPS_Math_Semi_To_Deg(lon); thisWaypoint->altitude = mps_altitude; @@ -914,7 +913,7 @@ mps_route_r(gbfile* mps_file, int mps_ver, route_head** rte) double mps_altitude = unknown_alt; double mps_depth = unknown_alt; - QScopedPointerrtename(gbfgetcstr(mps_file)); + QString rtename = gbfgetcstr(mps_file); #ifdef MPS_DEBUG fprintf(stderr, "mps_route_r: reading route %s\n", rtename); #endif @@ -959,7 +958,7 @@ mps_route_r(gbfile* mps_file, int mps_ver, route_head** rte) #endif rte_head = route_head_alloc(); - rte_head->rte_name = rtename.take(); + rte_head->rte_name = rtename; route_add_head(rte_head); *rte = rte_head; @@ -1541,7 +1540,7 @@ mps_track_r(gbfile* mps_file, int mps_ver, route_head** trk) (void)mps_ver; - QScopedPointertrkname (gbfgetcstr(mps_file)); + QString trkname = gbfgetcstr(mps_file); #ifdef MPS_DEBUG fprintf(stderr, "mps_track_r: reading track %s\n", trkname); #endif @@ -1563,7 +1562,7 @@ mps_track_r(gbfile* mps_file, int mps_ver, route_head** trk) #endif track_head = route_head_alloc(); - track_head->rte_name = trkname.take(); + track_head->rte_name = trkname; track_add_head(track_head); *trk = track_head; -- 2.30.2